package com.ggs.rg.ybjb.usermodule.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ggs.rg.ybjb.pojomodule.pojo.TComment;
import com.ggs.rg.ybjb.pojomodule.pojo.TOrder;
import com.ggs.rg.ybjb.usermodule.mapper.TCommentMapper;
import com.ggs.rg.ybjb.usermodule.mapper.TOrderMapper;
import java.util.HashMap;
import java.util.List;

/**
 * ClassName:OrderUtil
 * Package:com.ggs.rg.ybjb.adminmodule.util
 * Description:用户端订单工具类
 * @Date:2021/11/2 20:59
 * @Author:cxw
 */
public class OrderUtil {

    /**
     * 将获取的到的订单信息转换为json格式
     * @param tOrder
     * @return
     */
    public static JSONObject orderForJson(TOrder tOrder) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        String oid = tOrder.getOid();
        result.put("oid", oid);
        result.put("price",tOrder.getPrice());
        result.put("createTime",tOrder.getCreateTime());
        result.put("updateTime",tOrder.getUpdateTime());
        result.put("address",tOrder.getAddress());
        result.put("consignee_name",tOrder.getConsigneeName());
        result.put("consignee_phone",tOrder.getConsigneePhone());
        return  result;
    }

    /**
     * 将获取到的订单信息进行封装，返回给前端已付款订单需要的数据
     * @param tOrder 订单对象
     * @param tOrderMapper 操作数据库订单表的mapper
     * @return
     */
    public static JSONObject orderForJson(TOrder tOrder,String phone,TCommentMapper tCommentMapper,TOrderMapper tOrderMapper) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        String oid = tOrder.getOid();
        result.put("oid", oid);
        result.put("price",tOrder.getPrice());
        result.put("createTime",tOrder.getCreateTime());
        result.put("updateTime",tOrder.getUpdateTime());
        result.put("address",tOrder.getAddress());
        result.put("consignee_name",tOrder.getConsigneeName());
        result.put("consignee_phone",tOrder.getConsigneePhone());
//      这个list用来存放订单中的商品信息(订单可能有多个商品所以用list)
        List<HashMap<String,Object>> goodsList = tOrderMapper.selectOrdersInfo(oid);
        for (HashMap map:goodsList){
            if (map.get("image")!=null){
                String image = (String) map.get("image");
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                map.put("image",imageAddr);
                QueryWrapper<TComment> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("phone",phone).eq("pid",map.get("pid")).eq("oid",oid);
                if (tCommentMapper.selectOne(queryWrapper) != null)
                    map.put("isComment",1);
                else
                    map.put("isComment",0);
            }
        }
        result.put("goodsList",goodsList);
        return  result;
    }

    public static JSONObject orderForJson(TOrder tOrder,TOrderMapper tOrderMapper) {
        //遍历,将获取到的订单信息与地址信息放入json中
        JSONObject result = new JSONObject();
        String oid = tOrder.getOid();
        result.put("oid", oid);
        result.put("price",tOrder.getPrice());
        result.put("createTime",tOrder.getCreateTime());
//      这个list用来存放订单中的商品信息(订单可能有多个商品所以用list)
        List<HashMap<String,Object>> goodsList = tOrderMapper.selectOrdersInfo(oid);
        for (HashMap map:goodsList){
            if (map.get("image")!=null){
                String image = (String) map.get("image");
                String imageAddr = ImageUtil.getImageAddr(image, "9001");
                map.put("image",imageAddr);
                QueryWrapper<TComment> queryWrapper = new QueryWrapper<>();
            }
        }
        result.put("goodsList",goodsList);
        return  result;
    }
}
